package swing.utils;

import java.awt.Color;
import java.awt.MediaTracker;
import java.io.File;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.swing.DefaultCellEditor;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;

import swing.ui.IdNameListCellRenderer;
import swing.ui.IdNameTableCellRenderer;
import action.vo.IdName;


public class SwingUtils {
	
	public static void setComboBoxColumn(JTable table, TableColumn column,List<IdName> list) {
		
		JComboBox jComboBox = new JComboBox(list.toArray(new Object[0]));
		jComboBox.setRenderer(new IdNameListCellRenderer());
		
		column.setCellEditor(new DefaultCellEditor(jComboBox));
		column.setCellRenderer(new IdNameTableCellRenderer());
	}
	
	
	public static void fitTableColumns(JTable table) {   
        JTableHeader header = table.getTableHeader();   
        int rowCount = table.getRowCount();   
        Enumeration columns = table.getColumnModel().getColumns();   
        while (columns.hasMoreElements()) {
            TableColumn column = (TableColumn) columns.nextElement();   
            int col = header.getColumnModel().getColumnIndex(column.getIdentifier());   
            int width = (int) table.getTableHeader().getDefaultRenderer()   
                    .getTableCellRendererComponent(table,column.getIdentifier(), false, false, -1, col).getPreferredSize().getWidth();   
            for (int row = 0; row < rowCount; row++) {
                int preferedWidth = (int) table.getCellRenderer(row, col)   
                        .getTableCellRendererComponent(table,   
                                table.getValueAt(row, col), false, false, row,col).getPreferredSize().getWidth();   
                width = Math.max(width, preferedWidth);   
            }   
            header.setResizingColumn(column); // 此行很重要   
            column.setWidth(width + table.getIntercellSpacing().width);   
        }   
    }
	
	public static void colorTableReadonly(JTable table,Color color) {   
		
        int columnCount = table.getColumnCount();
        for(int columnIndex=0;columnIndex<columnCount;columnIndex++){
    		if(!table.getModel().isCellEditable(0, columnIndex)){
    			 TableColumn column = (TableColumn) table.getColumnModel().getColumn(columnIndex);
    			 DefaultTableCellRenderer cellRenderer = null;
    			 if(null != column.getCellRenderer()){
    				 cellRenderer = (DefaultTableCellRenderer)column.getCellRenderer();
    			 }
    			 else{
    				 cellRenderer = new DefaultTableCellRenderer();
    			 }
    			 cellRenderer.setBackground(color);
    			 column.setCellRenderer(cellRenderer);
    		}
    	}
    }
	
    //手动设置列宽   
	public static void fitTableColumns(JTable table, int[] columnWidths) {   
        for (int i = 0; i < columnWidths.length; i++) {   
            table.getColumnModel().getColumn(i).setPreferredWidth(   
                    columnWidths[i]);   
        }   
    }
	
	public static ImageIcon getOpenSwingImage(String name) {
        return getOpenSwingImage(name, null);
    }

	public static String imgSrc = "resource/image";
    public static ImageIcon getOpenSwingImage(String name,
                                              ImageIcon defaultIcon) {
        ImageIcon icon = null;
        try {
            java.net.URL url = ClassLoader.getSystemResource(
            		imgSrc + name);
            icon = new ImageIcon(url);
        }
        catch (Exception ex) {
        }
        if (icon == null
            || icon.getImageLoadStatus() != MediaTracker.COMPLETE
            || icon.getIconHeight() <= 0) {
            icon = defaultIcon;
        }
        return icon;
    }
    private static ResourceBundle rb;
    public static String getOpenResource(String key) {
        if (rb == null) {
            try{
                rb = ResourceBundle.getBundle(
                    "message",
                    Locale.getDefault(),
                    ClassLoader.getSystemClassLoader());
            }
            catch (Exception ex) {
                throw new NullPointerException(
                    "ERROR:CAN NOT FOUND RESOURCE FILE!  " +
                    new File("message_" + Locale.getDefault().toString() +
                             ".properties").getAbsolutePath());
            }
        }
        return rb.getString(key);
    }
}
